package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentMaterialCategory;
import com.mxpio.erp.common.equipment.service.EquipmentMaterialCategoryService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentMaterialCategoryController}", description = "备品分类")
@RestController
@RequestMapping("/erp/equipment/material/category/")
public class EquipmentMaterialCategoryController {

    @Autowired
    private EquipmentMaterialCategoryService equipmentMaterialCategoryService;

    @GetMapping("list")
    @Operation(summary = "备品分类列表", description = "获取备品分类列表（无分页）", method = "GET")
    public Result<List<EquipmentMaterialCategory>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentMaterialCategory> list = equipmentMaterialCategoryService.list(EquipmentMaterialCategory.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("tree")
    @Operation(summary = "备品分类树", description = "获取备品分类树", method = "GET")
    public Result<List<EquipmentMaterialCategory>> tree(Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentMaterialCategory> EquipmentMaterialCategorys = equipmentMaterialCategoryService.tree(EquipmentMaterialCategory.class, criteria);
        return Result.OK(EquipmentMaterialCategorys);
    }

    @GetMapping("childList/{pid}")
    @Operation(summary = "根据pid获取子节点备品分类列表", description = "根据pid获取备品分类列表", method = "GET")
    public Result<List<EquipmentMaterialCategory>> getChildList(@PathVariable(name = "pid", required = true) String pid) {
        Criteria c = Criteria.create().addCriterion("pid", Operator.EQ, pid);
        List<EquipmentMaterialCategory> list = equipmentMaterialCategoryService.list(EquipmentMaterialCategory.class, c);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "备品分类列表", description = "获取备品分类列表（分页）", method = "GET")
    public Result<Page<EquipmentMaterialCategory>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        //只查询父节点
        criteria = criteria.addCriterion("pid", Operator.EQ, "0");
        Page<EquipmentMaterialCategory> pageResult = equipmentMaterialCategoryService.listPage(EquipmentMaterialCategory.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取备品分类列表", description = "根据ID获取备品分类列表", method = "GET")
    public Result<EquipmentMaterialCategory> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentMaterialCategory EquipmentMaterialCategory = equipmentMaterialCategoryService.getById(EquipmentMaterialCategory.class, id);
        return Result.OK(EquipmentMaterialCategory);
    }

    @RequestMapping("add")
    @Operation(summary = "保存备品分类", description = "保存备品分类")
    public Result<EquipmentMaterialCategory> add(@RequestBody EquipmentMaterialCategory EquipmentMaterialCategory) {
        equipmentMaterialCategoryService.addEquipmentMatCategory(EquipmentMaterialCategory);
        return Result.OK(EquipmentMaterialCategory);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑备品分类", description = "编辑备品分类", method = "PUT")
    public Result<EquipmentMaterialCategory> edit(@RequestBody EquipmentMaterialCategory EquipmentMaterialCategory) {
        equipmentMaterialCategoryService.updateEquipmentMatCategory(EquipmentMaterialCategory);
        return Result.OK(EquipmentMaterialCategory);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除备品分类", description = "删除备品分类,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentMaterialCategory> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentMaterialCategoryService.deleteEquipmentMatCategory(key);
        }
        return Result.OK();
    }

}
